package com.ruoyi.web.controller.yf;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.yf.config.BarcodeConfig;
import com.ruoyi.common.config.YfGlobal;
import com.ruoyi.yf.tool.barcode.GenerateBarcode;
import com.ruoyi.yf.tool.barcode.Rules;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
/**
 * 条形码查询统计Controllerw
 *
 * @author XiaoYi...
 * @date 2020-01-07
 * 1414a3b0-a355-44a1-92e8-e22c78da3ddf
 */
@Controller
@RequestMapping("/yf/tagmanager")
//@DataSource(value = DataSourceType.SLAVE)
public class TagmanagerController extends BaseController {
    private String prefix = "yf/tagmanager";
    private String report = prefix + "/report";
    private String barcode = prefix + "/generate";

    @Autowired
    private YfGlobal yfGlobal;

    /*
     ********************************************报表页面**************************************************
     */


    /********************************工具************************************/


    /**
     * 生成条形码
     * 8cfc9ceb-d7bd-40e4-a14e-da00f39d5dc9
     */
    @RequiresPermissions("yf:barcode:view")
    @GetMapping("/barcode/view")
    public String barcode() {
        return barcode + "/barcode";
    }


    /**
     * 生成条码验证密码
     * @return
     * f15307d5-fa7a-44d5-9e11-c45117da8385
     */
    @RequiresPermissions("yf:barcode:pass")
    @PostMapping("/barcode/generate/pass")
    @ResponseBody
    public AjaxResult validationGeneratePassword(String pass, BarcodeConfig barcodeConfig) {
        if (pass != null && pass.equals(yfGlobal.getPassword())) {
            JSONObject obj = new JSONObject();
            obj.put("downloadurl", prefix + "/barcode/generate");
            return AjaxResult.success("密码验证成功！", obj);
        }
        return AjaxResult.error("密码验证错误！");
    }


    /**
     * 验证条形码规则正确否
     * @param pcode
     * @return
     * a6f6369e-c95a-40af-b64c-a8150545596b
     */
    @RequiresPermissions("yf:barcode:validation")
    @PostMapping("/barcode/validation")
    @ResponseBody
    public AjaxResult validationBarcode(@Param("pcode") String pcode) {
        System.out.println("code:" + pcode);
        boolean b = Rules.codeCheckLong(pcode);
        if (b)
            return AjaxResult.success("条码验证成功", b);
        return AjaxResult.error("条码验证失败", b);
    }


    //批量生成条码 返回下载地址
    //73a98c3e-b713-414f-835c-3491049c675d
    @RequiresPermissions("yf:barcode:generate")
    @PostMapping("/barcode/generate")
    @ResponseBody
    public AjaxResult generateBarcode(BarcodeConfig barcodeConfig) {
        System.out.println("form提交了" + barcodeConfig);
        SysUser user = ShiroUtils.getSysUser();
        JSONObject filenameObj = GenerateBarcode.generate(barcodeConfig, Global.getProfile(), user.getUserId().toString(), yfGlobal.getPassword());

        System.out.println(filenameObj);

        if (filenameObj.get("code").equals("1000")) {
            filenameObj.put("downloadurl", prefix + "/download/" + filenameObj.get("path"));
            return AjaxResult.success("生成成功", filenameObj);
        } else {
            return AjaxResult.error("配置信息失败", filenameObj);
        }

    }

    //生成一个条码
//    58ec08f3-dd3f-4594-b116-5f4bb4efa4c8
    @RequiresPermissions("yf:barcode:generate:i")
    @PostMapping("/barcode/i")
    @ResponseBody
    public AjaxResult generaBarcodei(String batch) {
        JSONObject obj = new JSONObject();
        String barcode = new Rules().barcode(true);
        obj.put("br", barcode);
        return AjaxResult.success("生成成功", obj);
    }

    /**************************************************************************************/



    //条码基本查询
//    ecbc8892-9c0a-40e0-9da8-cd5e32905f16
    @RequiresPermissions("yf:tagmanager:view")
    @GetMapping()
    public String tagmanager() {
        return prefix + "/dianziliandan";
    }



    /**
     * 本地资源通用下载
     * c611a678-31e0-4f0f-83a8-1b4a5b31ba1c
     */
    @GetMapping("/download/{name}")
    public void codeDownload(@PathVariable("name") String resource, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 本地资源路径
        String localPath = Global.getProfile();
        System.out.println("localPath:" + localPath);

        String downloadPath = localPath + File.separator + resource;
        System.out.println("downloadName:" + downloadPath);

        // 下载名称
        String downloadName = StringUtils.substringAfterLast(downloadPath, "/");

        System.out.println("downloadName:" + downloadPath);
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }
}
